import Mock from 'mockjs'
import floorDataMock from './floor_data'

// 模拟点位数据
const generateMockPoints = () => {
  const points = []
  const departments = ['教务处', '财务处', '人事处', '学生处', '后勤处', '校长办公室', '副校长办公室', '党委办公室']
  const roomTypes = ['办公室', '会议室', '资料室', '值班室', '接待室']
  
  for (let i = 1; i <= 100; i++) {
    const department = departments[Math.floor(Math.random() * departments.length)]
    const roomType = roomTypes[Math.floor(Math.random() * roomTypes.length)]
    const name = department + roomType
    const floor = Math.floor(Math.random() * 4) + 1
    
    points.push({
      id: 1000 + i,
      name: name,
      room_code: `Room-${floor}${String(i).padStart(2, '0')}`,
      floor: floor,
      staff: `员工${i}`,
      department: department,
      remark: `备注信息${i}`
    })
  }
  
  // 添加一些特殊测试数据
  points.push({
    id: 2001,
    name: '副校长办公室',
    room_code: 'A101',
    floor: 1,
    staff: '张副校长',
    department: '校办',
    remark: '校领导办公室'
  })
  
  points.push({
    id: 2002,
    name: '校长办公室',
    room_code: 'A102',
    floor: 1,
    staff: '李校长',
    department: '校办',
    remark: '校领导办公室'
  })
  
  points.push({
    id: 2003,
    name: '留学生办公室',
    room_code: 'B201',
    floor: 2,
    staff: '王老师',
    department: '国际交流处',
    remark: '负责留学生事务'
  })
  
  return points
}

const mockPoints = generateMockPoints()

// 模糊匹配函数
const fuzzyMatch = (text, keyword) => {
  if (!text || !keyword) return false
  const pattern = '.*' + keyword.split('').join('.*') + '.*'
  const regex = new RegExp(pattern, 'i')
  return regex.test(text)
}

export default [
  ...floorDataMock,
  {
    url: "/api/search_points",
    method: "post",
    response: (config) => {
      const { keyword } = config.body
      
      if (!keyword) {
        return {
          code: 200,
          msg: 'success',
          data: []
        }
      }
      
      // 使用模糊匹配过滤数据
      const filteredPoints = mockPoints.filter(point => 
        fuzzyMatch(point.name, keyword) ||
        fuzzyMatch(point.room_code, keyword) ||
        fuzzyMatch(String(point.id), keyword) ||
        fuzzyMatch(String(point.floor), keyword) ||
        fuzzyMatch(point.staff, keyword) ||
        fuzzyMatch(point.department, keyword) ||
        fuzzyMatch(point.remark, keyword)
      )
      
      return {
        code: 200,
        msg: 'success',
        data: filteredPoints
      }
    }
  },
  {
    url: "/api/record_route",
    method: "post",
    response: (config) => {
      const { start_id, end_id } = config.body
      console.log('记录路径:', start_id, '->', end_id)
      
      return {
        code: 200,
        msg: '路径记录成功',
        data: null
      }
    }
  },
  {
    url: "/api/hot_points",
    method: "get",
    response: () => {
      return {
        code: 200,
        msg: 'success',
        data: [
          { id: 1001, name: '校长办公室' },
          { id: 1002, name: '副校长办公室' },
          { id: 1003, name: '教务处' },
          { id: 1004, name: '财务处' }
        ]
      }
    }
  },
  {
    url: "/api/department_data",
    method: "get",
    response: () => {
      return {
        code: 200,
        msg: 'success',
        data: [
          { id: 1, name: '教务处' },
          { id: 2, name: '财务处' },
          { id: 3, name: '人事处' },
          { id: 4, name: '学生处' },
          { id: 5, name: '后勤处' }
        ]
      }
    }
  }
]